import 'package:flutter/material.dart';
import 'package:hkzf/model/user.dart';
import 'package:provider/provider.dart';

class Header extends StatelessWidget {
  const Header({Key key}) : super(key: key);
  Widget _notLoginBuilder(BuildContext context) {
    return Container(
      height: 95,
      decoration: BoxDecoration(color: Colors.green),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            height: 65,
            width: 65,
            margin: EdgeInsets.only(left: 15, right: 15),
            child: CircleAvatar(
              backgroundImage: NetworkImage(
                  "https://tva1.sinaimg.cn/large/006y8mN6ly1g6tbgbqv2nj30i20i2wen.jpg"),
            ),
          ),
          GestureDetector(
            onTap: () {
              Navigator.of(context).pushNamed('/login');
            },
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '登录/注册',
                  style: TextStyle(color: Colors.white, fontSize: 18),
                ),
                Text(
                  '登录后可以体验更多',
                  style: TextStyle(color: Colors.white, fontSize: 14),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget _loginBuilder(BuildContext context) {
    String userName =
        context.select((UserInfo value) => value).userInfo['nickName'];
    String avator =
        context.select((UserInfo value) => value).userInfo['avator'];
    print('avator');
    print(avator);
    // "https://tva1.sinaimg.cn/large/006y8mN6ly1g6tbnovh8jj30hr0hrq3l.jpg";

    return Container(
      height: 95,
      decoration: BoxDecoration(color: Colors.green),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
              height: 65,
              width: 65,
              margin: EdgeInsets.only(left: 15, right: 15),
              child: avator != ''
                  ? CircleAvatar(
                      backgroundImage: NetworkImage(avator),
                    )
                  : ClipOval(
                      child: Container(
                        decoration: BoxDecoration(color: Colors.blueAccent),
                        alignment: Alignment.center,
                        child: Text(
                          userName.length >= 2
                              ? userName.substring(0, 2)
                              : userName,
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                    )),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '$userName',
                style: TextStyle(color: Colors.white, fontSize: 18),
              ),
              GestureDetector(
                onTap: () {
                  Navigator.of(context).pushNamed('/usercenter');
                },
                child: Text(
                  '查看编辑个人资料',
                  style: TextStyle(color: Colors.white, fontSize: 14),
                ),
              ),
            ],
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return context.select((UserInfo userInfo) => userInfo.userInfo) != null
        ? _loginBuilder(context)
        : _notLoginBuilder(context);
  }
}
